package com.example.exam.controller;

import com.example.exam.exception.ServiceException;
import com.example.exam.pojo.User;
import com.example.exam.pojo.UserRole;
import com.example.exam.service.imp.UserService;
import com.example.exam.util.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/insertUser")
    public RespBean insertUser(@RequestBody @Valid  User user) {

        userService.insertUser(user);
        return RespBean.ok("用户添加成功");
    }

    @PostMapping("/updateUserById")
    public RespBean updateUserById(User user) {
        userService.updateUserById(user);
       return  RespBean.ok("用户修改成功");
    }

    @GetMapping("/selectUserById")
    public RespBean selectUserById(Long id) {
        User user=userService.selectUserById(id);
        return RespBean.ok("查询成功",user);
    }

    @GetMapping("/deleteUserById")
    public RespBean deleteUserById(Long id) {
        userService.deleteUserById(id);
        return  RespBean.ok("用户删除成功");
    }
}
